<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" border>
              <el-table-column label="序号" width="80" type="index" align="center" />
              <el-table-column label="角色名称" width="240" prop="name" align="center" />
              <el-table-column label="描述" prop="description" align="center" />
              <el-table-column label="操作" align="center">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="hAssignPer(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="q.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="q.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        :title="isEdit?'编辑角色':'新增角色'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        @close="hClose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog :visible.sync="perDialog" title="权限分配">
        <assignPermission v-if="perDialog" :cur-id="curId" @success="perDialog=false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import assignPermission from './assignPermission.vue'
import { addRoles, delRoles, getRolesList, updateRoles } from '@/api/settings'
export default {
  components: { assignPermission },
  data() {
    return {
      q: {
        page: 1,
        pagesize: 10
      },
      list: [],
      total: 0,
      roleForm: {
        name: '',
        description: ''
      },
      isEdit: false,
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      showDialog: false,
      perDialog: false,
      curId: ''
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    hAssignPer(id) {
      this.curId = id
      this.perDialog = true
    },
    async loadRoles() {
      const res = await getRolesList(this.q)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.q.pagesize = val
      this.q.page = 1
      this.loadRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.q.page = val
      this.loadRoles()
    },
    hSubmit() {
      this.$refs.roleForm.validate(async(vaild) => {
        if (!vaild) return
        await this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    hClose() {
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    async  doAdd() {
      try {
        const res = await addRoles(this.roleForm)
        if (res.code !== 10000) return
        this.$message.success(res.message)
        this.total++
        const pageMax = Math.ceil(this.total / this.q.pagesize)
        this.q.page = pageMax
        this.loadRoles()
        this.showDialog = false
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    async  doEdit() {
      try {
        const res = await updateRoles(this.roleForm)
        if (res.code !== 10000) return
        this.$message.success(res.message)
        this.loadRoles()
        this.showDialog = false
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    async hAdd() {
      try {
        this.isEdit = false
        this.showDialog = true
      } catch (error) {
        console.log(error)
      }
    },
    async  hEdit(row) {
      try {
        this.isEdit = true
        this.showDialog = true
        this.roleForm = { ...row }
      } catch (error) {
        console.log(error)
      }
    },
    hDel(id) {
      this.$confirm('确认删除该角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delRoles(id)
        if (res.code === 10000) {
          this.$message.success(res.message)
          if (this.list.length === 1) {
            this.q.page--
            if (this.q.page <= 0) {
              this.q.page = 1
            }
          }
          this.loadRoles()
        }
      }).catch((error) => {
        this.$message.error(error)
      })
    }
  }
}
</script>
